opioid_data_wide = read_csv(file="Wide_Master.csv")
## 
## -- Column specification --------------------------------------------------------
## cols(
##   .default = col_double(),
##   State = col_character(),
##   County = col_character(),
##   `GDP Education, Health, Social Assistance` = col_character()
## )
## i Use `spec()` for the full column specifications.
counties = read_sf(dsn ="C:\\Users\\zgord\\OneDrive - University of North Carolina at Chapel Hill\\Semesters\\Spring 2021\\STOR 320\\Project\\data\\cb_2018_us_county_5m\\cb_2018_us_county_5m.shp")
fips = read.csv("us-state-ansi-fips.csv")
# clean data. Done as a group


opioid_data_wide1 <- opioid_data_wide %>%
  filter(Year == '2016') %>%
  filter(!is.na(Total))

opioid_data_wide1 <- opioid_data_wide1 %>%
  mutate(HS_Or_Less = (HS_Grad + Less_Than_HS)/(HS_Grad + Less_Than_HS + Bachelor_Degree + Grad_Degree + Associates_Degree)) %>%
  mutate(HS_Or_Less_Quantile = ntile(HS_Or_Less, 4)) %>%
  mutate(Some_College = (Bachelor_Degree + Grad_Degree + Associates_Degree)/(HS_Grad + Less_Than_HS + Bachelor_Degree + Grad_Degree + Associates_Degree)) %>%
  mutate(Some_College_Quantile = ntile(Some_College, 4)) %>%
  mutate(Proportion_Non_US = Non_US_Born/Population) %>% 
  mutate(Non_US_Quantile = ntile(Proportion_Non_US, 20)) %>%
  mutate(PopDensity = Population/LandArea, Deaths_Per_100000 = (Total/Population) * 100000) %>%
  mutate(GDP_Per_Capita = `GDP Total`/Population) %>%
  mutate(GDP_EHSA_Per_Capita = as.numeric(`GDP Education, Health, Social Assistance`)/Population) %>%
  mutate(GDP_PerCap_Quantile = ntile(GDP_Per_Capita, 4)) %>%
  mutate(GDP_EHSA_PerCap_Quantile = ntile(GDP_EHSA_Per_Capita, 4)) %>%
  mutate(Social_Transportation = Bike + Walk + Carpool + Public) %>%
  mutate(AntiSocial_Transportation = Alone + Home)
## Warning: Problem with `mutate()` input `GDP_EHSA_Per_Capita`.
## i NAs introduced by coercion
## i Input `GDP_EHSA_Per_Capita` is `as.numeric(`GDP Education, Health, Social Assistance`)/Population`.
opioid_data_wide1
## # A tibble: 583 x 39
##    State County  Year Total Heroin Other Methadone Population LandArea  Bike
##    <chr> <chr>  <dbl> <dbl>  <dbl> <dbl>     <dbl>      <dbl>    <dbl> <dbl>
##  1 Alab~ Baldw~  2016    16     NA    12        NA     208563    1590.   0.1
##  2 Alab~ Cullm~  2016    12     NA    NA        NA      82471     735.  NA  
##  3 Alab~ Jeffe~  2016   116     80    31        14     659521    1111.   0.1
##  4 Alab~ Mobile  2016    18     NA    11        NA     414836    1229.   0.1
##  5 Alab~ Shelby  2016    17     10    NA        NA     210622     785.   0  
##  6 Alab~ St. C~  2016    10     NA    NA        NA      88019     632.  NA  
##  7 Alas~ Ancho~  2016    43     30    20        NA         NA      NA   NA  
##  8 Alas~ Fairb~  2016    10     NA    NA        NA     100605    7338.   0.7
##  9 Alas~ Kenai~  2016    11     NA    NA        NA      58506   16075.  NA  
## 10 Alas~ Matan~  2016    18     NA    13        NA         NA      NA    0.1
## # ... with 573 more rows, and 29 more variables: Carpool <dbl>, Alone <dbl>,
## #   Public <dbl>, Walk <dbl>, Home <dbl>, Income <dbl>, Non_US_Born <dbl>,
## #   Bachelor_Degree <dbl>, Grad_Degree <dbl>, HS_Grad <dbl>,
## #   Less_Than_HS <dbl>, Associates_Degree <dbl>, Unemployment <dbl>, `GDP
## #   Total` <dbl>, `GDP Education, Health, Social Assistance` <chr>,
## #   HS_Or_Less <dbl>, HS_Or_Less_Quantile <int>, Some_College <dbl>,
## #   Some_College_Quantile <int>, Proportion_Non_US <dbl>,
## #   Non_US_Quantile <int>, PopDensity <dbl>, Deaths_Per_100000 <dbl>,
## #   GDP_Per_Capita <dbl>, GDP_EHSA_Per_Capita <dbl>, GDP_PerCap_Quantile <int>,
## #   GDP_EHSA_PerCap_Quantile <int>, Social_Transportation <dbl>,
## #   AntiSocial_Transportation <dbl>
counties = counties %>%
  mutate(STATEFP = as.numeric(STATEFP))

counties.sf.data = opioid_data_wide1 %>% 
  filter(Year == 2016) %>% 
  left_join(fips, by = c("State" = "stname")) %>% 
  right_join(counties, by = c("st" = "STATEFP", "County" = "NAME")) %>% 
  mutate(county_name = paste(County,", ", State, sep ="")) %>%
  st_as_sf()

tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(counties.sf.data) +
  tm_polygons("Deaths_Per_100000",
              id = "county_name",
              title = "Opioid eaths per capita in 2016",
              popup.vars = c("Deaths Per 100,000: " = "Deaths_Per_100000",
                             "Population of county: " = "Population"),
              )